package com.web;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.data.SpittleRepository;
import com.domain.Spitter;
import com.domain.Spittle;
import com.sun.org.apache.bcel.internal.generic.NEW;

@Controller
@RequestMapping("/spitter")
public class SpittleController {
	/*这部分因为SpittleRepository只有接口，没有具体的类，自动注入会报错
	private SpittleRepository spittleRepository;
	//注入spittleRepository
	@Autowired
	public SpittleController(SpittleRepository spittleRepository){
		this.spittleRepository = spittleRepository;
	}
	//将spittle添加到模型中，然后返回视图名
	@RequestMapping(method = RequestMethod.GET)
	public String spittles(Model model){
		//model.addAttribute("spittleList",spittleRepository.findSpittles(Long.MAX_VALUE, 20));
		return "spittles";
	}
	*/
	//处理对“spitter/register的get请求”
	@RequestMapping(value="/register",method=RequestMethod.GET)
	public String showRegisterForm(Model model){
		model.addAttribute(new Spitter());
		return "registerForm";
	}
	
	//处理所提交的表单并注册新用户
	@RequestMapping(value="/register",method=RequestMethod.POST)
	public String processRegisteraction(@Valid Spitter spitter,Errors errors){
		if(errors.hasErrors()){
			return "registerForm";
		}
		return "redirect:/spitter/" + spitter.getUsername();
	}
}
